﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NS
{
    [HotUpdate]
    [Description("新品规则表选择供应商时携带出相关物料")]
    public class CarryingMaterialInformation : AbstractDynamicFormPlugIn
    {
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            if(e.Field.Key.Equals("F_WNEG_Base1"))
            {
                //初始化单据体
                this.View.Model.DeleteEntryData("F_WNEG_Entity");
                string F_WNEG_Base1 = "", FMATERIALID="";

                if (this.Model.GetValue("F_WNEG_Base1") != null)
                {
                    F_WNEG_Base1 = Convert.ToString((this.Model.GetValue("F_WNEG_Base1") as DynamicObject)["ID"]);
                }
                else
                {
                    return;
                }
                string SQL = "/*dialect*/" + string.Format(@"select t.FMATERIALID from T_BD_MATERIAL t 
                left join t_bd_MaterialPurchase t1 on t.FMATERIALID=t1.FMATERIALID
                left join T_BD_MATERIALGROUP t2 on t.FMATERIALGROUP=t2.FID
                where t.FUSEORGID='1' and t1.FDEFAULTVENDORID='{0}' AND T2.FID<>'116195'", F_WNEG_Base1);
                DynamicObjectCollection DynamData = DBUtils.ExecuteDynamicObject(this.Context, SQL);
                if(DynamData.Count>0)
                {
                    for(int i=0;i<DynamData.Count;i++)
                    {
                        FMATERIALID = Convert.ToString(DynamData[i]["FMATERIALID"]);
                        if (!FMATERIALID.Equals(""))
                        {
                            this.Model.CreateNewEntryRow("F_WNEG_Entity");
                            this.Model.SetValue("F_WNEG_Base", FMATERIALID, i);
                        }
                    }
                }
                //刷新,单据体界面
                this.View.UpdateView("F_WNEG_Entity");
            }
        }
    }
}
